package cn.fws.meap.user.web;

import java.util.List;

import cn.fws.framework.utils.StringUtils;
import cn.fws.meap.community.vo.ArticleListVo;
import cn.fws.meap.community.vo.ProblemListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.fws.meap.user.entity.Collect;
import cn.fws.meap.user.service.ICollectService;
import cn.fws.framework.web.contoller.BaseController;
import cn.fws.framework.web.page.PagingResult;
import cn.fws.framework.web.contoller.JsonResponse;

/**
 * 收藏记录 信息操作处理
 *
 * @author chenkh
 * @date 2019-03-07
 */
@RestController
@RequestMapping("/meap/collect")
@Api(tags = "收藏记录 信息操作处理")
public class CollectController extends BaseController{

    @Autowired
    private ICollectService collectService;

    /**
     * 查询收藏记录列表
     */
    //@RequiresPermissions("meap:collect:list")
    @ApiOperation(value = "查询收藏记录列表")
    @PostMapping("/getArticleList")
    public PagingResult<ArticleListVo> getArticleList(String openid, String articleType){
        startPage();
        List<ArticleListVo> list = collectService.selectArticleList(openid,articleType);
        return getPagingResult(list);
    }
    @ApiOperation(value = "查询收藏记录列表")
    @PostMapping("/getProblemList")
    public PagingResult<ProblemListVo> getProblemList(String openid, String problemType){
        startPage();
        List<ProblemListVo> list = collectService.getProblemList(openid,problemType);
        return getPagingResult(list);
    }

    /**
     * 新增保存收藏记录
     */
    //@RequiresPermissions("meap:collect:add")
    @ApiOperation(value = "新增保存收藏记录")
    @ApiImplicitParam(name = "collect", value = "新增保存收藏记录",dataType = "Collect")
    @PostMapping("/addSave")
    public JsonResponse addSave(Collect collect)	{
        if(StringUtils.isEmpty(collect.getArticleType())){
            return JsonResponse.error("404","文章类型不能为空");
        }else if(StringUtils.isEmpty(collect.getArticleId())){
            return JsonResponse.error("404","文章ID不能为空");
        }else if(StringUtils.isEmpty(collect.getOpenid())){
            return JsonResponse.error("404","用户ID不能为空");
        }else if(StringUtils.isEmpty(collect.getAuthorId())){
            return JsonResponse.error("404","作者ID不能为空");
        }else {
            //0 未收藏 1已收藏
            Collect collectCount = collectService.selectByarticleIdAndOpenId(collect.getArticleId(),collect.getOpenid());

            if(StringUtils.isNull(collectCount)){
                //收藏
                return JsonResponse.success("收藏成功",collectService.insert(collect));
            }else {
                //删除
                return   JsonResponse.success("取消收藏",collectService.deleteById(collectCount.getId()));
            }

        }

    }

    /**
     * 修改保存收藏记录
     */
    //@RequiresPermissions("meap:collect:edit")
    @ApiOperation(value = "修改保存收藏记录")
    @ApiImplicitParam(name = "collect", value = "修改保存收藏记录",dataType = "Collect")
    @PutMapping("/edit")
    public JsonResponse editSave(Collect collect)	{
        return toAjax(collectService.update(collect));
    }

    /**
     * 删除收藏记录
     */
    //@RequiresPermissions("meap:collect:remove")
    @ApiOperation(value = "删除收藏记录")
    @ApiImplicitParam(name = "ids", value = "根据id删除收藏记录",dataType = "String")
    @DeleteMapping( "/remove")
    public JsonResponse remove( String ids)	{
        return toAjax(collectService.deleteByIds(ids));
    }

}
